package com.tracy.lifeproject;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.tracy.lifeproject.fragment.DiscoveryFragment;
import com.tracy.lifeproject.fragment.HomeFragment;
import com.tracy.lifeproject.fragment.PersonalFragment;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    public int mCount = 0;
    public long mStartTimes;

    @InjectView(R.id.fl_container)
    FrameLayout mFlContainer;

    @InjectView(R.id.btn_home)
    ImageView mBtnHome;
    @InjectView(R.id.btn_discovery)
    ImageView mBtnDiscovery;
    @InjectView(R.id.btn_person)
    ImageView mBtnPerson;
    private int previousId = R.id.btn_home;
    private HomeFragment mHomeFragment;
    private DiscoveryFragment mDiscoveryFragment;
    private PersonalFragment mPersonalFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);

        initView();

    }

    private void initView() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        mHomeFragment = new HomeFragment();
        mDiscoveryFragment = new DiscoveryFragment();
        mPersonalFragment = new PersonalFragment();
        transaction.add(R.id.fl_container, mHomeFragment);
        transaction.add(R.id.fl_container, mDiscoveryFragment);
        transaction.add(R.id.fl_container, mPersonalFragment);
        hideFragment(transaction);
        transaction.show(mHomeFragment);
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mHomeFragment != null) {
            transaction.hide(mHomeFragment);
        }
        if (mDiscoveryFragment != null) {
            transaction.hide(mDiscoveryFragment);
        }
        if (mPersonalFragment != null) {
            transaction.hide(mPersonalFragment);
        }
    }

    @OnClick({R.id.btn_home, R.id.btn_discovery, R.id.btn_person})
    public void onClick(View view) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (view.getId()) {
            case R.id.btn_home:
                mBtnHome.setImageResource(R.mipmap.icon_home_selected);
                switch (previousId) {
                    case R.id.btn_person:
                        mBtnPerson.setImageResource(R.mipmap.icon_personal);
                        break;
                    case R.id.btn_discovery:
                        mBtnDiscovery.setImageResource(R.mipmap.icon_discovery);
                        break;
                }
                previousId = R.id.btn_home;
                hideFragment(transaction);
                transaction.show(mHomeFragment);
                transaction.commit();
                break;
            case R.id.btn_discovery:
                mBtnDiscovery.setImageResource(R.mipmap.icon_discovery_selected);
                switch (previousId) {
                    case R.id.btn_person:
                        mBtnPerson.setImageResource(R.mipmap.icon_personal);
                        break;
                    case R.id.btn_home:
                        mBtnHome.setImageResource(R.mipmap.icon_home);
                        break;
                }
                previousId = R.id.btn_discovery;
                hideFragment(transaction);
                transaction.show(mDiscoveryFragment);
                transaction.commit();
                break;
            case R.id.btn_person:
                mBtnPerson.setImageResource(R.mipmap.icon_personal_selected);
                switch (previousId) {
                    case R.id.btn_home:
                        mBtnHome.setImageResource(R.mipmap.icon_home);
                        break;
                    case R.id.btn_discovery:
                        mBtnDiscovery.setImageResource(R.mipmap.icon_discovery);
                        break;
                }
                previousId = R.id.btn_person;
                hideFragment(transaction);
                transaction.show(mPersonalFragment);
                transaction.commit();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (mCount == 0) {
            mCount++;
            mStartTimes = System.currentTimeMillis();
            Toast.makeText(MainActivity.this, "再按一次退出LIFE", Toast.LENGTH_SHORT).show();
            return;
        }
        if (System.currentTimeMillis() - mStartTimes < 2000) {
            finish();
        } else {
            mStartTimes = System.currentTimeMillis();
            Toast.makeText(MainActivity.this, "再按一次退出LIFE", Toast.LENGTH_SHORT).show();
        }
    }
}
